/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.notification.impl;

import com.ceair.flight.refund.service.mailAndSms.MailAndSMSMessageProducer;
import com.ceair.flight.refund.service.notification.ISimpleNotification;
import com.ceair.flight.refund.util.GsonUtil;
import com.ceair.muservice.common.dto.queue.MailAndSMSMessage;
import com.ceair.muservice.common.enums.message.TemplateNameEnum;
import com.ceair.muss.outside.httpinvoke.bean.input.CheckInOneRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Colin_You
 */
abstract class AbstractNotification implements ISimpleNotification {

    private static final Logger LOG = LoggerFactory.getLogger(AbstractNotification.class);
    @Resource(name = "mailAndSMSMessageProducer")
    private MailAndSMSMessageProducer messageSender;

    public static final String TYPE_MAIL = "MAIL";

    public static final String TYPE_SMS = "SMS";

//    @Autowired
//    private IEmailService emailService;
//    @Autowired
//    private IEmsService emsService;

    @Override
    public void notify(final Map args) {
        try {
            MailAndSMSMessage message = buildMessage(args);
            if (message != null) {
                String messageEntity = GsonUtil.toJson(message);
                LOG.debug("Message entity: {}", messageEntity);
                messageSender.sendMessage(message);
            }
        } catch (Exception ex) {
            LOG.error("Send notification error", ex);
        }
    }

    public MailAndSMSMessageProducer getMessageSender() {
        return messageSender;
    }

    MailAndSMSMessage buildMessage(final Map args) {
        MailAndSMSMessage message = new MailAndSMSMessage();
        message.setServerTy(getTemplateName(args).getCode());
        if (null != args.get(CHANNEL)){
            message.setChannel(args.get(CHANNEL).toString());
            args.remove(CHANNEL);
        }
        if (null != args.get(COUNTRY)){
            message.setCountry(args.get(COUNTRY).toString());
            args.remove(COUNTRY);
        }
        if (null != args.get(LANGUAGE)){
            message.setLanguage(args.get(LANGUAGE).toString());
            args.remove(LANGUAGE);
        }

        Map<String, Object> emailArgs = new HashMap<>(10);
//        emailArgs.put(TEMPLEAT_NAME, getTemplateName(args).getCode());
//        emailArgs.putAll(getDefaultValue(args));
        emailArgs.putAll(args);
        message.setMap(emailArgs);

        Long savedMsgId = null;
        if (message.getMap() != null && message.getMap().get(MOBILES) != null) {
            message.setMsgTP(TYPE_SMS);
//            savedMsgId = saveSms(emailArgs);
        } else if (message.getMap() != null && message.getMap().get(RECEIVER) != null) {
            message.setMsgTP(TYPE_MAIL);
//            savedMsgId = saveEmail(emailArgs);
        }
//        if (savedMsgId == null) {
//            LOG.error("Message not saved before send it, message will not be sent");
//            return null;
//        }
//        message.setMsgId(savedMsgId);
        return message;
    }

//    private Long saveSms(Map emailArgs) {
//        try {
//            Long sendSmsId = emsService.saveSMS(emailArgs);
//            return sendSmsId;
//        } catch (Exception ex) {
//            LOG.error("Save email error with arguments:{} ", emailArgs, ex);
//        }
//        return null;
//    }
//
//    private Long saveEmail(Map emailArgs) {
//        try {
//            Long sendMailId = emailService.saveMail(emailArgs);
//            return sendMailId;
//        } catch (Exception ex) {
//            LOG.error("Save email error with arguments:{} ", emailArgs, ex);
//        }
//        return null;
//    }

    /**
     * set message default parameters
     *
     * @param args
     * @return
     */
    Map<String, Object> getDefaultValue(final Map args) {
        //default, no default value
        return Collections.EMPTY_MAP;
    }

    abstract TemplateNameEnum getTemplateName(final Map args);

}
